package com.yh.workbill.ui.countinfo.mvp;

import com.yh.workbill.model.WorkBill;
import com.yh.workbill.network.OkHttpService;
import com.yh.workbill.ui.home.HomeViewModel;
import com.yh.workbill.ui.search.mvp.Search_Model;

import java.util.List;

import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class CountInfo_Presenter implements LifecycleObserver {
    private CountInfo_View view;
    private CountInfo_Model model;

    public CountInfo_Presenter(CountInfo_View view) {
        this.view = view;
        this.model = new CountInfo_Model();
    }


    public void getData(HomeViewModel viewModel, boolean isRefresh, boolean isLoadMore, String companyname, String unitname, String year, String month, int pageindex, int pagenumber) {
        view.showDialog("正在加载数据中...", true);
        model.getCountInfo(companyname, unitname, year, month, pageindex, pagenumber, new Search_Model.SearchCallBack() {
            @Override
            public void getData(List<WorkBill> data) {
                view.hideDialog();
                WorkBill[] workBills = data.toArray(new WorkBill[data.size()]);
                if (isRefresh) {
                    if (viewModel.getWorkBilldata().getValue() != null) {
                        viewModel.getWorkBilldata().getValue().clear();
                    }
                    for (WorkBill bean : workBills) {
                        bean.setCheck(true);
                    }
                    viewModel.addworkBilldatas(0, workBills);
                    view.refresh();
                } else if (isLoadMore) {
                    int index = viewModel.getWorkBilldata().getValue().size();
                    viewModel.addworkBilldatas(index, workBills);
                    view.loadMore();
                }
                view.getData(data);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.fail(msg);
            }
        });
    }

    public void getFilePath(String json, String year, String month, String companyname, String unitname, int pageindex, int pagenumber) {
        view.showDialog("正在生成云单...", true);
        model.downFile(json, year, month, companyname, unitname, pageindex, pagenumber, new CountInfo_Model.OnFileCallBack() {
            @Override
            public void onSuccess(String data) {
                view.createExcelFile(data);
                view.hideDialog();
            }

            @Override
            public void fail(String msg) {
                view.fail(msg);
                view.hideDialog();
            }
        });
    }

    public void downloadFile(String path) {
        view.showDialog("正在下载云单中...", true);
        model.download(path, new CountInfo_Model.OnFileDownCalBack() {
            @Override
            public void onSuccess(String data, String url) {
                ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                    view.hideDialog();
                    view.downFileSuccess(data,url);
                });
            }

            @Override
            public void fail(String msg) {
                ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                    view.hideDialog();
                    view.fail(msg);
                });
            }
        });
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestory() {
        if (this.view != null) {
            this.view = null;
        }
    }

}
